package com.liao.badminton.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.liao.badminton.entity.ActivityParticipator;
import com.liao.badminton.dto.ActivityParticipatorDto;
import com.liao.badminton.vo.ActivityParticipatorVo;
import com.liao.badminton.mapper.ActivityParticipatorMapper;
import com.liao.badminton.service.ActivityParticipatorService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
/**
* @author liaocheng
* @date 2024-11-15
* description:活动参与者表
*/
@Component
@Log4j2
public class ActivityParticipatorServiceImpl  extends ServiceImpl<ActivityParticipatorMapper, ActivityParticipator> implements ActivityParticipatorService {


    /**
    * 新增
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest saveActivityParticipator(ActivityParticipatorDto param) throws ProjectException{
        ActivityParticipator activityParticipator = param.invertEntity();
        EntityUtils.setCreatAndUpdateInfo(activityParticipator);
        this.save(activityParticipator);
        return ResponseRest.suc("saveActivityParticipator", activityParticipator.getId());
    }
    /**
    * 删除
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest deletesActivityParticipator(ActivityParticipatorDto param) throws ProjectException {
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "activityParticipator", "id", id));
        }
        return ResponseRest.suc("deletesActivityParticipator",true);
    }

    @Override
    public List<ActivityParticipatorVo> findByActivityId(Long activityId) {
        return baseMapper.findByActivityId(activityId);
    }

    @Override
    public ResponseRest<Boolean> updates(List<ActivityParticipatorDto> params) {
        boolean b = this.updateBatchById(params.stream().map(ActivityParticipatorDto::invertEntity).collect(Collectors.toList()));
        if (b) {
            params.forEach(entity -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "activityParticipator", "id", entity.getId()));
        }
        return ResponseRest.suc("updates",true);
    }

    /**
    * 修改
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest updateActivityParticipator(ActivityParticipatorDto param)  throws ProjectException{
        ActivityParticipator activityParticipator = param.invertEntity();
        EntityUtils.setUpdatedInfo(activityParticipator);
        boolean bool = this.updateById(activityParticipator);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "activityParticipator", "id", param.getId());
        }
        return ResponseRest.suc("updateActivityParticipator",true);
    }
    /**
    * 查询列表
    */
    @Override
    public ResponseRest findActivityParticipatorByPageParam(PageParam<ActivityParticipatorDto> pageParam)  throws ProjectException{
        CheckUtil.checkValue(pageParam,"pageParam");
        ResponseRest resp = ResponseRest.suc("findActivityParticipatorByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize(),"create_time desc");
        //LambdaQueryWrapper<ActivityParticipator> query = new LambdaQueryWrapper<>();
        List<ActivityParticipator> list = baseMapper.findByPageParam(pageParam);
        List<ActivityParticipatorVo> collect = list.stream().parallel().map(entity -> ActivityParticipatorVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
    * 查询详情
    */
    @Override
    public ResponseRest findDetail(ActivityParticipatorDto param) throws ProjectException {
        Object getDate = RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "activityParticipator", "id", param.getId());
        if (isNotEmpty(getDate)) {
        RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "activityParticipator", "id", param.getId());
        return ResponseRest.suc("findDetail", (ActivityParticipatorVo) getDate);
        }
        ActivityParticipatorVo vo = null;
        ActivityParticipator db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = ActivityParticipatorVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA,"activityParticipator", "id", param.getId(),vo, null);
        }
        return ResponseRest.suc("findDetail", vo);
    }

}
